﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Lolitemchanger
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private CGestionItem m_cgiItems = new CGestionItem("ENGLISH");
        private List<CItem> m_ciCharactersItem = new List<CItem>();
        private List<CItem> m_ciAllitem = new List<CItem>();
        List<Items> m_lsItems = new List<Items>();
        private CGestionZip zip = new CGestionZip();
        private CGestionIni iniConfiguration = new CGestionIni(Environment.CurrentDirectory + @"\Configuration.ini");
        private CGestionIni iniCharacters = new CGestionIni(Environment.CurrentDirectory + @"\Characters.ini");

        public Window1()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            try
            {
                Icon = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\lol-icon.ico", UriKind.RelativeOrAbsolute));
                
                Image imgConfiguration = new Image();
                imgConfiguration.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\Configuration.ico", UriKind.RelativeOrAbsolute));
                btn_configuration.Content = imgConfiguration;
                btn_configuration.ToolTip = "Configuration";
                
                Image imgSwitch = new Image();
                imgSwitch.Source = BitmapFrame.Create(new Uri(Environment.CurrentDirectory + @"\Images\Reload.ico", UriKind.RelativeOrAbsolute));
                btn_sauvegarder.Content = imgSwitch;
                btn_sauvegarder.ToolTip = "Cliquez ici pour échanger les items sélectionnés.\n\t/!\\ Un seul item à la fois !";

                List<BitmapImage> biIcons = LoadIcons();
                List<BitmapImage> biItems = LoadItems();
                List<Items> lsHeroes = new List<Items>();

                foreach (BitmapImage icons in biIcons)
                {
                    string strHeroe = "";
                    string[] split = icons.ToString().Split('/');
                    foreach (string s in split)
                        if (s.Contains(".jpg"))
                        {
                            string[] sSplit = s.Split('.');
                            strHeroe = sSplit[0];
                        }
                    lsHeroes.Add(new Items { path = icons.ToString(), name = strHeroe });
                }

                ls_characters.ItemsSource = lsHeroes;

                foreach (BitmapImage items in biItems)
                {
                    string strItems = "";
                    string[] split = items.ToString().Split('/');
                    foreach (string s in split)
                        if (s.Contains(".jpg"))
                        {
                            string[] sSplit = s.Split('.');
                            strItems = sSplit[0];
                        }
                    m_lsItems.Add(new Items { path = items.ToString(), name = strItems });
                }

                btn_sauvegarder.IsEnabled = false;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private List<BitmapImage> LoadIcons()
        {
            List<BitmapImage> biIcons = new List<BitmapImage>();
            DirectoryInfo diIconsDir = new DirectoryInfo(Environment.CurrentDirectory + @"\Images\Icons");

            foreach (FileInfo fiIcons in diIconsDir.GetFiles("*.jpg"))
            {
                Uri uri = new Uri(fiIcons.FullName);
                biIcons.Add(new BitmapImage(uri));
            }

            return biIcons;
        }

        private List<BitmapImage> LoadItems()
        {
            List<BitmapImage> biIcons = new List<BitmapImage>();
            DirectoryInfo diIconsDir = new DirectoryInfo(Environment.CurrentDirectory + @"\Images\Items");

            foreach (FileInfo fiIcons in diIconsDir.GetFiles("*.jpg"))
            {
                Uri uri = new Uri(fiIcons.FullName);
                biIcons.Add(new BitmapImage(uri));
            }

            return biIcons;
        }

        private void ls_characters_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                btn_sauvegarder.IsEnabled = false;
                ls_characteritems.Items.Clear();
                ls_newcharacteritem.Items.Clear();
                m_ciCharactersItem.Clear();
                m_ciAllitem.Clear();

                Items iHeroe = (Items)ls_characters.SelectedItem;

                zip.UnZip(iniConfiguration.RecupererValeur("Configuration", "path"), Environment.CurrentDirectory, iniCharacters.RecupererValeur("Heroes", iHeroe.name));

                m_cgiItems.SetLanguage(iniConfiguration.RecupererValeur("Configuration", "LANGUAGE").ToUpper());
                m_cgiItems.SetFilePath(Environment.CurrentDirectory + @"\DATA\Characters");
                m_cgiItems.SetCurrentCharacter(iniCharacters.RecupererValeur("Heroes", iHeroe.name));
                m_ciCharactersItem = m_cgiItems.UpdateCharacterItem();

                for (int i = 0; i < m_ciCharactersItem.Count; i++)
                    for (int j = 0; j < m_lsItems.Count; j++)
                        if (m_lsItems[j].name == m_ciCharactersItem[i].GetName())
                        {
                            Items iCharacterItem = new Items { path = m_lsItems[j].path, name = i + 1 + "-" + m_lsItems[j].name };
                            ls_characteritems.Items.Add(iCharacterItem);
                        }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (Directory.Exists(Environment.CurrentDirectory + @"\DATA"))
                Directory.Delete(Environment.CurrentDirectory + @"\DATA", true);
        }

        private void ls_characteritems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btn_sauvegarder.IsEnabled = false;
            m_ciAllitem.Clear();
            m_ciAllitem = m_cgiItems.GetAllItem();
            ls_newcharacteritem.Items.Clear();

            if (ls_characteritems.SelectedItem != null)
                for (int i = 0; i < m_lsItems.Count; i++)
                    ls_newcharacteritem.Items.Add(m_lsItems[i]);
        }

        private void ls_newcharacteritem_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ls_characteritems.SelectedItem != null && ls_newcharacteritem.SelectedItem != null)
                btn_sauvegarder.IsEnabled = true;
        }

        private void btn_sauvegarder_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ls_newcharacteritem.SelectedItem != null && ls_characteritems.SelectedItem != null)
                {
                    btn_sauvegarder.IsEnabled = false;
                    Mouse.OverrideCursor = Cursors.Wait;
                    var req = (from enr in m_ciAllitem
                               where enr.GetName() == ((Items)ls_newcharacteritem.SelectedItem).name
                               select enr).SingleOrDefault();

                    if (req != null)
                    {
                        m_cgiItems.EditCharacterItem(ls_characteritems.SelectedIndex, req.GetNumber(), req.GetName());
                    }
                    m_ciCharactersItem.Clear();
                    m_ciCharactersItem = m_cgiItems.UpdateCharacterItem();

                    zip.Zip(Environment.CurrentDirectory + @"\DATA\Characters", iniConfiguration.RecupererValeur("Configuration", "path"), m_cgiItems.GetCurrentCharacter());

                    ls_characteritems.Items.Clear();
                    for (int i = 0; i < m_ciCharactersItem.Count; i++)
                        for (int j = 0; j < m_lsItems.Count; j++)
                            if (m_lsItems[j].name == m_ciCharactersItem[i].GetName())
                            {
                                Items iCharacterItem = new Items { path = m_lsItems[j].path, name = i + 1 + "-" + m_lsItems[j].name };
                                ls_characteritems.Items.Add(iCharacterItem);
                            }

                    Mouse.OverrideCursor = Cursors.Arrow;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }

        }

        private void btn_configuration_Click(object sender, RoutedEventArgs e)
        {
            Configuration config = new Configuration();
            config.WindowStyle = WindowStyle.None;
            config.Owner = this;
            config.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            config.Width = this.Width;
            config.Height = this.Height;
            config.ShowDialog();

            ls_characteritems.UpdateLayout();
            ls_characters.UpdateLayout();
            ls_newcharacteritem.UpdateLayout();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CGestionIni IniFile = new CGestionIni(Environment.CurrentDirectory + @"\Configuration.ini");

            if (!IniFile.Exist())
            {
                Configuration config = new Configuration();
                config.WindowStyle = WindowStyle.None;
                config.Owner = this;
                config.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                config.Width = this.Width;
                config.Height = this.Height;
                config.ShowDialog();
            }
        }
    }

    public class Items
    {
        public string path { get; set; }
        public string name { get; set; }

        public Items()
        {
            path = string.Empty;
            name = string.Empty;
        }
    }
}
